<template>
    <div class="container">
        <div class="title">
            <span>修改</span>
            <span class="back" @click="back">
        <i class="iconfont icon-fanhui"></i> 返回
      </span>
        </div>
        <el-divider></el-divider>
        <br/>
        <el-form label-width="120px"
                 :model="form"
                 status-icon
                 ref="form"
                 @submit.native.prevent>
            <el-form-item label="图片" prop="img_id">
                <upload-imgs
                        ref="uploadEle"
                        :value="imgValue"
                        :rules="rules"
                        :multiple="true"
                        :sortable="true"
                        :min-num="1"
                        :max-num="5"
                />
                <div>推荐上传  </div>
            </el-form-item>
            <el-form-item label="分类" prop="sort_id">
                <el-select v-model="form.sort_id" filterable placeholder="请选择">
                    <el-option
                            v-for="item in sortArr"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="名称" prop="name">
                <el-input size="medium" v-model="form.name" placeholder="请填写名称"></el-input>
            </el-form-item>
            <el-form-item label="价格" prop="price">
                <el-input size="medium" type="number" v-model="form.price" placeholder="请填写名称"></el-input>
            </el-form-item>
            <el-form-item label="库存" prop="number">
                <el-input size="medium" type="number" v-model="form.number" placeholder="请填写库存"></el-input>
            </el-form-item>
            <el-form-item label="排序" prop="order_sort">
                <el-input size="medium" type="number" v-model="form.order_sort" placeholder="请填写排序(值越大越在前)"></el-input>
            </el-form-item>
            <el-form-item label="排序" prop="recommend">
                <el-radio v-model="form.recommend" label="0">不推荐首页</el-radio>
                <el-radio v-model="form.recommend" label="1">推荐首页</el-radio>
            </el-form-item>
            <el-form-item label="上下架" prop="up_down">
                <el-radio v-model="form.up_down" label="1">上架商品</el-radio>
                <el-radio v-model="form.up_down" label="2">下架商品</el-radio>
            </el-form-item>
            <el-form-item label="商品简介" prop="company">
                <div class="lin-wrap">
                    <tinymce  :defaultContent="form.content" @change="change" />
                </div>
            </el-form-item>
            <el-form-item class="submit">
                <el-button type="primary" @click="submitForm('form')">保 存</el-button>
                <el-button @click="resetForm('form')">重 置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import goods from '@/models/goods'
import UploadImgs from '@/components/base/upload-imgs'
import Tinymce from '@/components/base/tinymce'

export default {
  components: {
    UploadImgs,
    Tinymce,
  },
  props: {
    goodsId: {
      type: Number,
    },
  },
  data() {
    return {
      sortArr: [],
      imgValue: [],
      form: {
        sort_id: '',
        name: '',
        price: '',
        number: '',
        order_sort: '',
        recommend: '0',
        up_down: '1',
        content: '',
      },
      rules: {
        maxSize: 0.2,
      },
    }
  },
  mounted() {
    this.getInfo()
    this.getSortAll()
  },
  methods: {
    async getSortAll() {
      this.sortArr = await goods.getGoodsSortAll()
    },
    // 获取详情
    async getInfo() {
      const info = await goods.getGoodsInfo(this.goodsId)
      info.imgs.forEach((item) => {
        this.imgValue.push({
          delete: false,
          id: item.id,
          imgId: item.img_id,
          display: item.img.url,
        })
      })
      this.form = {
        sort_id: info.sort_id,
        name: info.name,
        price: info.price,
        number: info.number,
        order_sort: info.order_sort,
        recommend: String(info.recommend),
        up_down: String(info.up_down),
        content: info.content,
      }
    },
    // eslint-disable-next-line no-unused-vars
    async submitForm(formName) {
      const self = this
      const imgValue = await self.$refs.uploadEle.getValue()
      const imgs = []
      imgValue.forEach((item) => {
        imgs.push({ img_id: item.imgId })
      })
      // eslint-disable-next-line no-unused-vars
      const info = {
        id: this.goodsId,
        sort_id: self.form.sort_id,
        imgs,
        name: self.form.name,
        price: self.form.price,
        number: self.form.number,
        recommend: self.form.recommend,
        up_down: self.form.up_down,
        order_sort: self.form.order_sort,
        content: self.form.content,
      }
      try {
        // eslint-disable-next-line no-unused-vars
        const res = await goods.updateGoods(info)
        if (res.error_code === 0) {
          this.$message.success(`${res.msg}`)
          this.back()
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 重置表单
    resetForm() {
      this.getInfo()
    },
    back() {
      this.$emit('editClose')
    },
    change(val) {
      this.form.content = val
    },
  },
}
</script>

<style lang="scss" scoped>
    .el-divider--horizontal {
        margin: 0
    }

    .container {
        .title {
            height: 59px;
            line-height: 59px;
            color: $parent-title-color;
            font-size: 16px;
            font-weight: 500;
            text-indent: 40px;

            .back {
                float: right;
                margin-right: 40px;
                cursor: pointer;
            }
        }

        .wrap {
            padding: 20px;
        }

        .submit {
            float: left;
        }
    }
</style>
